package com.example.easycharge.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.easycharge.entity.ChargePile;
import com.example.easycharge.notRepEntity.WaitQueue;
import com.example.easycharge.repository.ChargePileRepository;
import com.example.easycharge.service.ChargeService;
import com.example.easycharge.service.PileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "充电桩接口组")
@Slf4j
@RestController
@RequestMapping("pile")
public class PileController {
    @Autowired
    private ChargePileRepository chargePileRepository;
    @Autowired
    private PileService pileService;
    @Autowired
    private ChargeService chargeService;

    @Operation(description = "查询单个充电桩的情况")
    @GetMapping("admin/{pileId}")
    public ChargePile getChargePileById(@PathVariable("pileId") long pileId) {
        return chargePileRepository.findChargePileByChargePileId(pileId);
    }

    @Operation(description = "充电桩在安装完成之后需要激活，激活完之后才可以使用")
    @PutMapping("admin/activate/{pileId}")
    public JSONObject activatePile(@PathVariable("pileId") long pileId) {
        return pileService.addNewQueue(pileId);
    }

    @Operation(description = "工作中的充电桩可以被关闭（出于某些原因），关闭后的充电桩队列中的车辆会被自动移除（但是还是推荐管理员手动移除）")
    @PutMapping("admin/shutOff/{pileId}")
    public JSONObject shutOffPile(@PathVariable("pileId") long pileId) {
        return pileService.removeQueue(pileId);
    }

    @Operation(description = "将充电桩设置为故障")
    @PutMapping("admin/error/{pileId}")
    public JSONObject setErrorPile(@PathVariable("pileId") long pileId) {
        return pileService.setErrorPile(pileId);
    }

    @Operation(description = "将充电桩从故障中恢复")
    @PutMapping("admin/recovery/{pileId}")
    public JSONObject setRecoveryPile(@PathVariable("pileId") long pileId) {
        return pileService.setRecoveryPile(pileId);
    }

    @Operation(description = "分页查询所有的充电桩的情况，可用的排序依据有【chargePower、installDate、workHours、chargeHours】")
    @GetMapping("admin/{orderBy}/{page}/{pageNum}/{asc}")
    public Page<ChargePile> getOrderedPile(@PathVariable("orderBy") String orderBy,
                                           @PathVariable("page") int page,
                                           @PathVariable("pageNum") int pageNum,
                                           @PathVariable("asc") boolean asc) {
        Pageable pageable = PageRequest.of(page - 1, pageNum, asc ? Sort.Direction.ASC : Sort.Direction.DESC, orderBy);
        return chargePileRepository.findAll(pageable);
    }

    @Operation(description = "删除充电桩的接口，很少会用到，实际情况大概就是拆了个充电桩？")
    @DeleteMapping("admin/{pileId}")
    public JSONObject deletePileById(@PathVariable("pileId") long pileId) {
        return pileService.deletePile(pileId);
    }

    @Operation(description = "查询队列状态")
    @GetMapping("admin/queue")
    public List<WaitQueue> getAllWaitQueue() {
        return chargeService.getWaitQueueList();
    }
}
